using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using static GZ.Projects.WCS_SVC.WMSHttpEntity;
using System.Configuration;
using GZ.Common.Code;

namespace GZ.Projects.WCS_SVC
{
	public class WMSHttpHelper
	{
		/// <summary>
		/// 获取接口IP地址和端口，WMS网页接口
		/// </summary>
		private static string IPAddress1 = "";

		/// <summary>
		/// 获取接口IP地址和端口，WMS（IM01-04）
		/// </summary>
		private static string IPAddress2 =ConfigurationManager.AppSettings["WMsPort"];

		#region 接口地址
		/// <summary>
		/// IM01接口
		/// </summary>
		private static string urlIM01 = @"http://" + IPAddress2 + @"/c2m/api/order/create";
		/// <summary>
		/// IM02接口
		/// </summary>
		private static string urlIM02 = @"http://" + IPAddress2 + @"/c2m/api/order/action";
		/// <summary>
		/// IM03接口
		/// </summary>
		private static string urlIM03 = @"http://" + IPAddress2 + @"/c2m/api/material/action";

		/// <summary>
		/// 登录
		/// </summary>
		private static string urlLogin = @"http://" + IPAddress1 + @"/account/admin/login";
		/// <summary>
		/// 获取收货单号，作业单号
		/// </summary>
		private static string urlNoList = @"http://" + IPAddress1 + @"/manage/api/HuaXHandle/QuerySap_Srm_Code?Type=";
		/// <summary>
		/// 库区列表
		/// </summary>
		private static string urlAreaLists = @"http://" + IPAddress1 + @"/manage/warehouse/area/list";
		/// <summary>
		/// 平库出库指引（显示出库列表）
		/// </summary>
		private static string urlOutList = @"http://" + IPAddress1 + @"/manage/api/HuaXHandle/FlatGuid";
		#endregion

		
		/// <summary>
		/// IM01.作业任务创建C
		/// </summary>
		public static WCreateOrderResponse GetIM01(WCreateOrderRequest input, out string msg)
		{
			msg = string.Empty;
			try
			{
				WMSResult<List<WCreateOrderResponse>> rst = null;
				string json = Json.ToJson(input);

				msg += $"\r\n\t[Step]报文：{json}\r\n\t[url]请求地址：{urlIM01}\r\n\t[Step]返回结果：";

				string httprst = new HttpClient().HttpPost(urlIM01, json, out string message);
				msg += message + httprst;
				if (string.IsNullOrEmpty(message))
				{
					rst = Json.ToObject<WMSResult<List<WCreateOrderResponse>>>(httprst) as WMSResult<List<WCreateOrderResponse>>;
					if (rst != null && rst?.code == 200)
					{
						return rst?.data[0];
					}
				}

				return null;
			}
			catch (Exception ex)
			{
				msg += ex.Message;
				return null;
			}
		}

		/// <summary>
		/// IM02.作业任务操作C
		/// </summary>
		public bool GetIM02(WOperationOrderRequest input, out string msg)
		{
			msg = string.Empty;
			try
			{
				//初始化
				string jsonParam = Json.ToJson(input);
				string httprst = new HttpClient().HttpPost(urlIM02, jsonParam, out  msg);
				msg += $"\r\n\t[Step]报文：{jsonParam}\r\n\t[url]请求地址：{urlIM02}\r\n\t[Step]返回结果：{httprst}";
				WMSResult<List<WOperationOrderResponse>> response = Json.ToObject<WMSResult<List<WOperationOrderResponse>>>(httprst) as WMSResult<List<WOperationOrderResponse>>;
				if (response == null)
				{
					msg += "获取WMS接口数据异常";
					return false;
				}
				if (response.code != 200)
				{
					msg += "获取WMS接口数据失败:" + response.message;
					return false;
				}
				if (response != null && response.data[0].ope_result == 1)
				{
					msg += "获取WMS接口数据失败:" + response.data[0].ope_memo;
					return false;
				}
				if(response.data[0].ope_result == 0)
				{
					//	string position=response.data[0].reverse_1;
					//	stationLoc=position;
					//	if(!string.IsNullOrEmpty(position))
					//	{
					//		AutoScanApp ausc = new AutoScanApp();
					//		IMainMissionRepository mainMissionRepo = new MainMissionRepository();
					//		MainMissionEntity mainMissionList = mainMissionRepo.FindEntity(t=>t.task_no==request.task_no );
					//		if(mainMissionList!=null)
					//		{
					//			if(mainMissionList.end_position!=position)
					//			{
					//				mainMissionList.reverse_2=mainMissionList.end_position;
					//				mainMissionList.tunnel_no=position.Substring(4, 1);
					//				mainMissionList.end_position=position;
					//			}
					//			mainMissionList.reverse_4="1";
					//			bool resultms = ausc.MainMissionUpdate(mainMissionList, out string err);
					//			if (!resultms)
					//			{
					//				Conn.WHLog.Error(1, "wms库位变更", 1, $"\r\n\t写入数据库失败[{mainMissionList.task_no}]{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss:fff")}");
					//				return false;
					//			}
					//		}
					//	}
					return true;
				}
				return false;
			}
			catch (Exception ex)
			{
				msg += "获取当前库存异常:" + ex.Message;
				return false;
			}
		}

		/// <summary>
		/// IM03.物料查询及绑定
		/// </summary>
		//public static WSearchBindMaterialResponse GetIM03(WSearchBindMaterialRequest input, out string msg)
		//{
		//	msg = string.Empty;
		//	try
		//	{
		//		WMSResult<List<WSearchBindMaterialResponse>> rst = null;
		//		//input.ope_device_no = UserDevno;
		//		//input.ope_person = UserNameR;
		//		string jsonParam = Json.ToJson(input);
		//		//url03 = "";
		//		msg += $"\r\n\t[Step]报文：{jsonParam}\r\n\t[url]请求地址：{urlIM03}\r\n\t[Step]返回结果：";
				
		//		var httprst = new HttpClient().HttpPost(urlIM03, jsonParam, out string message);
		//		msg += message;
		//		if (string.IsNullOrEmpty(message))
		//		{
		//			//var result = Json.ToObject<WMSResult<string>>(httprst);
		//			//if (result.code != 200)
		//			//{
		//			//    return null;
		//			//}

		//			rst = Json.ToObject<WMSResult<List<WSearchBindMaterialResponse>>>(httprst) as WMSResult<List<WSearchBindMaterialResponse>>;
		//			if (rst != null && rst?.code == 200)
		//			{
		//				return rst?.data[0];
		//			}
		//		}

		//		return null;

		//	}
		//	catch (Exception ex)
		//	{
		//		msg += ex.Message;
		//		return null;
		//	}
		//}

		/// <summary>
		/// 登录
		/// </summary>
		public static LoginResponse LoginWms(LoginRequest input, out string msg)
		{
			msg = string.Empty;
			try
			{
				WMSResult<LoginResponse> rst = null;
				//input.ope_device_no = UserDevno;
				//input.ope_person = UserNameR;
				string jsonParam = Json.ToJson(input);
				//url03 = "";
				msg += $"\r\n\t[Step]报文：{jsonParam}\r\n\t[url]请求地址：{urlLogin}\r\n\t[Step]返回结果：";

				string httprst = new HttpClient().HttpPost(urlLogin, jsonParam, out string message);
				msg += message + httprst;
				if (string.IsNullOrEmpty(message))
				{
					rst = Json.ToObject<WMSResult<LoginResponse>>(httprst) as WMSResult<LoginResponse>;
					if (rst != null && rst?.code == 200)
					{
						return rst?.data;
					}
				}

				return null;

			}
			catch (Exception ex)
			{
				msg += ex.Message;
				return null;
			}
		}

		/// <summary>
		/// 获取订单收货单列表，显示文本输入后选择
		/// </summary>
		/// <param name="inputType">1/2(订单号/收货单号)</param>
		//public static List<string> NoList(string inputType, out string msg)
		//{
		//	msg = string.Empty;
		//	try
		//	{
		//		WMSResult<List<string>> rst = null;

		//		string httprst = new HttpClient().HttpGet(urlNoList, inputType, out string message);
		//		msg += message + httprst;
		//		if (string.IsNullOrEmpty(message))
		//		{
		//			rst = Json.ToObject<WMSResult<List<string>>>(httprst) as WMSResult<List<string>>;
		//			if (rst != null && rst?.code == 200)
		//			{
		//				return rst?.data;
		//			}
		//		}

		//		return null;

		//	}
		//	catch (Exception ex)
		//	{
		//		msg += ex.Message;
		//		return null;
		//	}
		//}

		/// <summary>
		/// 获取库区列表，绑定下拉框
		/// </summary>
		//public static List<AreaLists> GetAreaLists(AreaListRequest input, out string msg)
		//{
		//	msg = string.Empty;
		//	try
		//	{
		//		WMSResult<AreaListResponse> rst = null;
		//		//input.ope_device_no = UserDevno;
		//		//input.ope_person = UserNameR;
		//		string jsonParam = Json.ToJson(input);
		//		//url03 = "";
		//		msg += $"\r\n\t[Step]报文：{jsonParam}\r\n\t[url]请求地址：{urlAreaLists}\r\n\t[Step]返回结果：";

		//		string httprst = new HttpClient().HttpPost(urlAreaLists, jsonParam, out string message);
		//		msg += message + httprst;
		//		if (string.IsNullOrEmpty(message))
		//		{
		//			rst = Json.ToObject<WMSResult<AreaListResponse>>(httprst) as WMSResult<AreaListResponse>;
		//			if (rst != null && rst?.code == 200)
		//			{
		//				return rst?.data.list;
		//			}
		//		}

		//		return null;

		//	}
		//	catch (Exception ex)
		//	{
		//		msg += ex.Message;
		//		return null;
		//	}
		//}

		/// <summary>
		/// 获取库区列表，绑定下拉框
		/// </summary>
		//public static List<outlist> GetOutList(OutListRequest input, out string msg)
		//{
		//	msg = string.Empty;
		//	try
		//	{
		//		WMSResult<List<outlist>> rst = null;
		//		//input.ope_device_no = UserDevno;
		//		//input.ope_person = UserNameR;
		//		string jsonParam = Json.ToJson(input);
		//		//url03 = "";
		//		msg += $"\r\n\t[Step]报文：{jsonParam}\r\n\t[url]请求地址：{urlOutList}\r\n\t[Step]返回结果：";

		//		string httprst = new HttpClient().HttpPost(urlOutList, jsonParam, out string message);
		//		msg += message + httprst;
		//		if (string.IsNullOrEmpty(message))
		//		{
		//			rst = Json.ToObject<WMSResult<List<outlist>>>(httprst) as WMSResult<List<outlist>>;
		//			if (rst != null && rst?.code == 200)
		//			{
		//				return rst?.data;
		//			}
		//		}

		//		return null;

		//	}
		//	catch (Exception ex)
		//	{
		//		msg += ex.Message;
		//		return null;
		//	}
		//}


		#region 异步调用
		/// <summary>
		/// 登录
		/// </summary>
		public static async Task<Tuple<LoginResponse, string>> LoginWmsNew(LoginRequest input)
		{
			var msg = string.Empty;
			try
			{
				WMSResult<LoginResponse> rst = null;
				//input.ope_device_no = UserDevno;
				//input.ope_person = UserNameR;
				string jsonParam = Json.ToJson(input);
				//url03 = "";
				msg += $"\r\n\t[Step]报文：{jsonParam}\r\n\t[url]请求地址：{urlLogin}\r\n\t[Step]返回结果：";
				HttpClient httpClient = new HttpClient();
				var httprst = await httpClient.HttpPostAsync(urlLogin, jsonParam);
				msg += httprst.Item1 + httprst.Item2;
				if (string.IsNullOrEmpty(httprst.Item2))
				{
					rst = Json.ToObject<WMSResult<LoginResponse>>(httprst.Item1) as WMSResult<LoginResponse>;
					if (rst != null && rst?.code == 200)
					{
						return new Tuple<LoginResponse, string>(rst?.data, msg);
					}
				}
				return new Tuple<LoginResponse, string>(null, msg); ;
			}
			catch (Exception ex)
			{
				msg += ex.Message;
				return null;
			}
		}

		/// <summary>
		/// IM01.作业任务创建C
		/// </summary>
		public static async Task<Tuple<WCreateOrderResponse, string>> GetIM01new(WCreateOrderRequest input)
		{
			var msg = string.Empty;
			try
			{
				WMSResult<List<WCreateOrderResponse>> rst = null;
				string json = Json.ToJson(input);

				msg += $"\r\n\t[Step]报文：{json}\r\n\t[url]请求地址：{urlIM01}\r\n\t[Step]返回结果：";

				var httprst = await new HttpClient().HttpPostAsync(urlIM01, json);
				msg += httprst.Item1 + httprst.Item2;
				if (string.IsNullOrEmpty(httprst.Item1))
				{
					rst = Json.ToObject<WMSResult<List<WCreateOrderResponse>>>(httprst.Item1) as WMSResult<List<WCreateOrderResponse>>;
					if (rst != null && rst?.code == 200)
					{
						return new Tuple<WCreateOrderResponse, string>(rst?.data[0], msg);
					}
				}

				return new Tuple<WCreateOrderResponse, string>(null, msg);
			}
			catch (Exception ex)
			{
				msg += ex.Message;
				return new Tuple<WCreateOrderResponse, string>(null, msg);
			}
		}

		/// <summary>
		/// IM03.物料查询及绑定
		/// </summary>
		//public static async Task<Tuple<WSearchBindMaterialResponse, string>> GetIM03New(WSearchBindMaterialRequest input)
		//{
		//	var msg = string.Empty;
		//	try
		//	{
		//		WMSResult<List<WSearchBindMaterialResponse>> rst = null;
		//		//input.ope_device_no = UserDevno;
		//		//input.ope_person = UserNameR;
		//		string jsonParam = Json.ToJson(input);
		//		//url03 = "";
		//		msg += $"\r\n\t[Step]报文：{jsonParam}\r\n\t[url]请求地址：{urlIM03}\r\n\t[Step]返回结果：";
		//		var HttpClient = new HttpClient();
		//		var httprst = await HttpClient.HttpPostAsync(urlIM03, jsonParam);
		//		msg += httprst.Item2 + httprst.Item1;
		//		if (string.IsNullOrEmpty(httprst.Item2))
		//		{
		//			rst = Json.ToObject<WMSResult<List<WSearchBindMaterialResponse>>>(httprst.Item1) as WMSResult<List<WSearchBindMaterialResponse>>;
		//			if (rst != null && rst?.code == 200)
		//			{
		//				return new Tuple<WSearchBindMaterialResponse, string>(rst?.data[0], msg);
		//			}
		//		}
		//		return new Tuple<WSearchBindMaterialResponse, string>(null, msg);
		//	}
		//	catch (Exception ex)
		//	{
		//		msg += ex.Message;
		//		return new Tuple<WSearchBindMaterialResponse, string>(null, msg);
		//	}
		//}
		#endregion
	}
}